package cn.com.se.sharepictrue.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.ListView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.com.se.sharepictrue.R;
import cn.com.se.sharepictrue.adapter.MineCommentListAdapter;
import cn.com.se.sharepictrue.bean.MineCommentBean;
import cn.com.se.sharepictrue.business.CallBack;
import cn.com.se.sharepictrue.business.NetworkResponse;
import cn.com.se.sharepictrue.business.ShareApi;
import cn.com.se.sharepictrue.quickadapter.BaseAdapterHelper;
import cn.com.se.sharepictrue.utils.ToastUtils;
import cn.com.se.sharepictrue.widget.LoadMoreListener;

/**
 * Created by Administrator on 2015/12/17.
 */
public class MineCommentActivity extends ActionBarActivity implements SwipeRefreshLayout.OnRefreshListener,MineCommentListAdapter.CommentItemListener {

    @Bind(R.id.tb_mine_comment_title)
    Toolbar mTbMineCommentTitle;
    @Bind(R.id.ll_mine_comment_list)
    ListView mLlMineCommentList;
    @Bind(R.id.srl_mine_comment_refresh)
    SwipeRefreshLayout mSrlMineCommentRefresh;

    private List<MineCommentBean> mBeans;
    private MineCommentListAdapter mAdapter;

    private LoadMoreListener mLoadMoreListener;
    private final int mLoadInLastRow = 4;
    private int nowPage = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_comment);
        ButterKnife.bind(this);
        initToolbar();
        initLoadMoreListener();
        initRefreshView();
    }

    private void initToolbar() {
        setSupportActionBar(mTbMineCommentTitle);
        mTbMineCommentTitle.setTitleTextColor(Color.WHITE);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }

        return super.onOptionsItemSelected(item);
    }


    private void initRefreshView() {
        mSrlMineCommentRefresh.setOnRefreshListener(this);
        mSrlMineCommentRefresh.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);
        mSrlMineCommentRefresh.post(new Runnable() {
            @Override
            public void run() {
                mSrlMineCommentRefresh.setRefreshing(true);
                initList();
            }
        });
    }

    private void initLoadMoreListener() {
        mLoadMoreListener = new LoadMoreListener(mLoadInLastRow) {
            @Override
            public void load() {
                nowPage++;
                ShareApi.getMyComment(MineCommentActivity.this, nowPage, new CallBack<NetworkResponse<List<MineCommentBean>>>() {
                    @Override
                    public void onFail() {
                        ToastUtils.show(MineCommentActivity.this, "加载失败..");
                    }

                    @Override
                    public void onResponse(NetworkResponse<List<MineCommentBean>> response) {
                        if (!response.isStatus()) {//加载失败
                            ToastUtils.show(MineCommentActivity.this, response.getError());
                        } else {
                            addToList(response.getData());
                            mLoadMoreListener.setLoading(false);
                        }
                    }
                });
            }
        };
    }

    private void addToList(List<MineCommentBean> data) {
        if (data != null) {
            mAdapter.addAll(data);
            mAdapter.notifyDataSetChanged();
        }
    }

    private void initList()
    {
        nowPage = 1;
        ShareApi.getMyComment(MineCommentActivity.this, nowPage, new CallBack<NetworkResponse<List<MineCommentBean>>>() {
            @Override
            public void onStart() {
                super.onStart();
                mSrlMineCommentRefresh.setRefreshing(true);
            }

            @Override
            public void onFail() {
                mSrlMineCommentRefresh.setRefreshing(false);
                ToastUtils.show(MineCommentActivity.this, "加载失败..");
            }

            @Override
            public void onResponse(NetworkResponse<List<MineCommentBean>> response) {
                mSrlMineCommentRefresh.setRefreshing(false);
                if (!response.isStatus())
                    ToastUtils.show(MineCommentActivity.this, response.getError());
                else {
                    mBeans = response.getData();
                    initListData();
                }
            }
        });
    }

    private void initListData()
    {
        if (mBeans != null && mBeans.size() > 0)
        {
            mAdapter = new MineCommentListAdapter(MineCommentActivity.this, R.layout.item_mine_comment, mBeans);
            mAdapter.setCommentItemListener(this);
            mLlMineCommentList.setAdapter(mAdapter);
            mLlMineCommentList.setOnScrollListener(mLoadMoreListener);
        }
    }

    @Override
    public void onRefresh() {
        initList();
    }

    @Override
    public void shareClick(BaseAdapterHelper helper, MineCommentBean item) {
        Intent intent = new Intent(MineCommentActivity.this, ShareDetailActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("key", item.getShare());
        intent.putExtra("item", bundle);
        intent.putExtra("position", 0);
        startActivity(intent);
    }
}
